<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 4] 4.8 Relational Comparison Operators</TITLE>
<META NAME="author" CONTENT="Mark Grand">
<META NAME="date" CONTENT="Thu Jul 31 13:10:24 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="java">
<META NAME="title" CONTENT="Java Language Reference">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java Language Reference" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch04_07.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 4<br>Expressions</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch04_09.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JLR2-CH-4-SECT-8">4.8 Relational Comparison Operators</A></h2>

<P CLASS=para>
<A NAME="CH04.RELAT1"></A><A NAME="CH04.RELAT2"></A><A NAME="CH04.EQUAL3"></A>The relational
comparison operators in Java are used for less than (<tt CLASS=literal>&lt;</tt>),
less than or equal to (<tt CLASS=literal>&lt;=</tt>), greater than
or equal to (<tt CLASS=literal>&gt;=</tt>), greater than (<tt CLASS=literal>&gt;</tt>),
and <tt CLASS=literal>instanceof</tt> comparison operations. They
may appear in a relational expression:


<p>
<img align=middle src="./figs/jlr0418.gif" alt="[Graphic: Figure from the text]" width=425 height=75 border=0>

<P CLASS=para>
The
relational comparison operators are equal in precedence and are
evaluated from left to right. The <tt CLASS=literal>&lt;</tt>, <tt CLASS=literal>&lt;=</tt>,
<tt CLASS=literal>&gt;=</tt>, and <tt CLASS=literal>&gt;</tt> operators are numerical
comparison operators, while <tt CLASS=literal>instanceof</tt> is a type
comparison operator. All of these operators produce <tt CLASS=literal>boolean</tt>
values.

<P CLASS=para>
<b>References</b>
<A HREF="ch04_07.htm#JLR2-CH-4-SECT-7">Shift Operators</A>;
<A HREF="ch04_14.htm#JLR2-CH-4-SECT-14">Order of Operations</A>;
<I CLASS=emphasis>Type</I> 3

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JLR2-CH-4-SECT-8.1">Less-Than Operator &lt;</A></h3>

<P CLASS=para>
The less-than operator &lt; performs a comparison between its operands
and returns a <tt CLASS=literal>boolean</tt>
value. It returns the pure value <tt CLASS=literal>true</tt> if its
left operand is less than its right operand; otherwise the operator
returns the pure value <tt CLASS=literal>false</tt>. The <tt CLASS=literal>&lt;</tt>
operator may appear as part of a relational expression. The
less-than operator never throws an exception.

<P CLASS=para>
The types of
both operands of the less-than operator must be arithmetic types,
or a compile-time error occurs. The <tt CLASS=literal>&lt;</tt> operator
may perform type conversions on its operands:
<P>
<UL CLASS=itemizedlist>
<li CLASS=listitem>If either operand is of type <tt CLASS=literal>double</tt>, then the other operand
is converted to <tt CLASS=literal>double</tt>.

<P>
<li CLASS=listitem>Otherwise,
if either operand is of type <tt CLASS=literal>float</tt>, the other
operand is converted to <tt CLASS=literal>float</tt>.

<P>
<li CLASS=listitem>Otherwise,
if either operand is of type <tt CLASS=literal>long</tt>, the other
operand is converted to <tt CLASS=literal>long</tt>.

<P>
<li CLASS=listitem>Otherwise,
both operands are converted to <tt CLASS=literal>int</tt>.

<P>
</UL>
<P CLASS=para>
The
comparison of any two arithmetic values produces <tt CLASS=literal>true</tt>
if the value of the left operand is less than the value of the right
operand; otherwise the comparison produces <tt CLASS=literal>false</tt>.
The comparison of floating-point data is governed by the following
additional rules:
<P>
<UL CLASS=itemizedlist>
<li CLASS=listitem>If either operand is not-a-number
(NaN), the comparison produces <tt CLASS=literal>false</tt>.

<P>
<li CLASS=listitem>Negative
infinity is the most negative value. If the left operand is negative
infinity, the comparison produces <tt CLASS=literal>true</tt>, unless
the right operand is also negative infinity, in which case the comparison
produces <tt CLASS=literal>false</tt>.

<P>
<li CLASS=listitem>Positive infinity
is the most positive value. If the right operand is positive infinity,
the comparison produces <tt CLASS=literal>true</tt>, unless the left
operand is also positive infinity, in which case the comparison
produces <tt CLASS=literal>false</tt>.

<P>
<li CLASS=listitem>Positive and negative
zero are treated as equal, so <tt CLASS=literal>-0.0 &lt; 0.0</tt>
produces <tt CLASS=literal>false</tt>.

<P>
</UL>
<P CLASS=para>
<b>References</b>
<A HREF="ch03_01.htm#JLR2-CH-3-SECT-1.1">Arithmetic Types</A>

</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JLR2-CH-4-SECT-8.2">Less-Than-Or-Equal-To Operator &lt;=</A></h3>

<P CLASS=para>
The less-than-or-equal-to operator <tt CLASS=literal>&lt;=</tt> performs
a comparison
between its operands and returns a <tt CLASS=literal>boolean</tt> value.
It returns the pure value <tt CLASS=literal>true</tt> if its left operand
is less than or equal to its right operand; otherwise the operator
returns the pure value <tt CLASS=literal>false</tt>. The <tt CLASS=literal>&lt;=</tt>
operator may appear as part of a relational expression.
The less-than-or-equal-to operator never throws an exception.

<P CLASS=para>
The
types of both operands of the less-than-or-equal-to operator must
be arithmetic types, or a compile-time error occurs. The <tt CLASS=literal>&lt;=</tt>
operator may perform type conversions on its operands:

<P>
<UL CLASS=itemizedlist>
<li CLASS=listitem>If either operand is of type <tt CLASS=literal>double</tt>, then the other operand
is converted to <tt CLASS=literal>double</tt>.

<P>
<li CLASS=listitem>Otherwise,
if either operand is of type <tt CLASS=literal>float</tt>, the other
operand is converted to <tt CLASS=literal>float</tt>.

<P>
<li CLASS=listitem>Otherwise,
if either operand is of type <tt CLASS=literal>long</tt>, the other
operand is converted to <tt CLASS=literal>long</tt>.

<P>
<li CLASS=listitem>Otherwise,
both operands are converted to <tt CLASS=literal>int</tt>.

<P>
</UL>
<P CLASS=para>
The
comparison of any two arithmetic values produces <tt CLASS=literal>true</tt>
if the value of the left operand is less than or equal to the value
of the right operand; otherwise the comparison produces <tt CLASS=literal>false</tt>.
The comparison of floating-point data is governed by the following
additional rules:
<P>
<UL CLASS=itemizedlist>
<li CLASS=listitem>If either operand is not-a-number
(NaN), the comparison produces <tt CLASS=literal>false</tt>.

<P>
<li CLASS=listitem>Negative
infinity is the most negative value. If the left operand is negative
infinity, the comparison always produces <tt CLASS=literal>true</tt>.

<P>
<li CLASS=listitem>Positive infinity is the most positive value. If the right
operand is positive infinity, the comparison always produces <tt CLASS=literal>true</tt>.

<P>
<li CLASS=listitem>Positive and negative zero are treated as equal, so
<tt CLASS=literal>0.0 &lt;= -0.0</tt> produces <tt CLASS=literal>true</tt>.

<P>
</UL>
<P CLASS=para>
<b>References</b>
<A HREF="ch03_01.htm#JLR2-CH-3-SECT-1.1">Arithmetic Types</A>

</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JLR2-CH-4-SECT-8.3">Greater-Than-Or-Equal-To Operator &gt;=</A></h3>

<P CLASS=para>
The greater-than-or-equal-to operator <tt CLASS=literal>&gt;=</tt> performs
a comparison
between its operands and returns a <tt CLASS=literal>boolean</tt> value.
It returns the pure value <tt CLASS=literal>true</tt> if its left operand
is greater than or equal to its right operand; otherwise the operator
returns the pure value <tt CLASS=literal>false</tt>. The <tt CLASS=literal>&gt;=</tt>
operator may appear as part of a relational expression. 
The greater-than-or-equal-to operator never throws an exception.

<P CLASS=para>
The types of both operands of the greater-than-or-equal-to operator
must be arithmetic types, or a compile-time error occurs.
The <tt CLASS=literal>&gt;=</tt> operator may perform type conversions
on its operands:
<P>
<UL CLASS=itemizedlist>
<li CLASS=listitem>If either operand is of type <tt CLASS=literal>double</tt>, then the other operand
is converted to <tt CLASS=literal>double</tt>.

<P>
<li CLASS=listitem>Otherwise,
if either operand is of type <tt CLASS=literal>float</tt>, the other
operand is converted to <tt CLASS=literal>float</tt>.

<P>
<li CLASS=listitem>Otherwise,
if either operand is of type <tt CLASS=literal>long</tt>, the other
operand is converted to <tt CLASS=literal>long</tt>.

<P>
<li CLASS=listitem>Otherwise,
both operands are converted to <tt CLASS=literal>int</tt>.

<P>
</UL>
<P CLASS=para>
The
comparison of any two arithmetic values produces <tt CLASS=literal>true</tt>
if the value of the left operand is greater than or equal to the
value of the right operand; otherwise the comparison produces <tt CLASS=literal>false</tt>.
The comparison of floating-point data is governed by the following
additional rules:
<P>
<UL CLASS=itemizedlist>
<li CLASS=listitem>If either operand is not-a-number
(NaN), the comparison produces <tt CLASS=literal>false</tt>.

<P>
<li CLASS=listitem>Negative
infinity is the most negative value. If the right operand is negative
infinity, the comparison always produces <tt CLASS=literal>true</tt>.

<P>
<li CLASS=listitem>Positive infinity is the most positive value. If the left
operand is positive infinity, the comparison always produces <tt CLASS=literal>true</tt>.

<P>
<li CLASS=listitem>Positive and negative zero are treated as equal, so <tt CLASS=literal>-0.0
&gt;= 0.0</tt> produces <tt CLASS=literal>true</tt>.

<P>
</UL>
<P CLASS=para>
<b>References</b>
<A HREF="ch03_01.htm#JLR2-CH-3-SECT-1.1">Arithmetic Types</A>

</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JLR2-CH-4-SECT-8.4">Greater-Than Operator &gt;</A></h3>

<P CLASS=para>
The greater-than operator <tt CLASS=literal>&gt;</tt>
performs a comparison between its operands and
returns a <tt CLASS=literal>boolean</tt>
value. It returns the pure value <tt CLASS=literal>true</tt> if its
left operand is greater than its right operand; otherwise the operator
returns the pure value <tt CLASS=literal>false</tt>.
The <tt CLASS=literal>&gt;</tt>
operator may appear as part of a relational expression. The greater-than
operator never throws an exception.

<P CLASS=para>
The types of
both operands of the greater-than operator must be arithmetic types,
or a compile-time error occurs. The <tt CLASS=literal>&gt;</tt> operator
may perform type conversions on its operands:
<P>
<UL CLASS=itemizedlist>
<li CLASS=listitem>If either operand is of type <tt CLASS=literal>double</tt>, then the other operand
is converted to <tt CLASS=literal>double</tt>.

<P>
<li CLASS=listitem>Otherwise,
if either operand is of type <tt CLASS=literal>float</tt>, the other
operand is converted to <tt CLASS=literal>float</tt>.

<P>
<li CLASS=listitem>Otherwise, if either operand is of type <tt CLASS=literal>long</tt>, the other
operand is converted to <tt CLASS=literal>long</tt>.

<P>
<li CLASS=listitem>Otherwise,
both operands are converted to <tt CLASS=literal>int</tt>.

<P>
</UL>
<P CLASS=para>
The
comparison of any two arithmetic values produces <tt CLASS=literal>true</tt>
if the value of the left operand is greater than the value of the
right operand; otherwise the comparison produces <tt CLASS=literal>false</tt>.
The comparison of floating-point data is governed by the following
additional rules:
<P>
<UL CLASS=itemizedlist>
<li CLASS=listitem>If either operand is not-a-number
(NaN), the comparison produces <tt CLASS=literal>false</tt>.

<P>
<li CLASS=listitem>Negative
infinity is the most negative value. If the right operand is negative
infinity, the comparison produces <tt CLASS=literal>true</tt>, unless
the left operand is also negative infinity, in which case the comparison
produces <tt CLASS=literal>false</tt>.

<P>
<li CLASS=listitem>Positive infinity
is the most positive value. If the left operand is positive infinity,
the comparison produces <tt CLASS=literal>true</tt>, unless the right
operand is also positive infinity, in which case the comparison
produces <tt CLASS=literal>false</tt>.

<P>
<li CLASS=listitem>Positive and negative
zero are treated as equal, so <tt CLASS=literal>0.0 &gt; -0.0</tt> produces
<tt CLASS=literal>false</tt>.

<P>
</UL>
<P CLASS=para>
<b>References</b>
<A HREF="ch03_01.htm#JLR2-CH-3-SECT-1.1">Arithmetic Types</A>

</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JLR2-CH-4-SECT-8.5">The instanceof Operator</A></h3>

<P CLASS=para>
The <tt CLASS=literal>instanceof</tt> operator performs a type comparison
between its operands and returns a <tt CLASS=literal>boolean</tt> value.
It returns the pure value <tt CLASS=literal>true</tt> if the object
referred to by the left operand can be cast to the type specified
as the right operand; otherwise the operator returns the pure value
<tt CLASS=literal>false</tt>. If the value of the left operand is <tt CLASS=literal>null</tt>,
the <tt CLASS=literal>instanceof</tt> operator returns the pure value
<tt CLASS=literal>false</tt>. The <tt CLASS=literal>instanceof</tt> operator
may appear as part of a relational expression. The <tt CLASS=literal>instanceof</tt>
operator never throws an exception.

<P CLASS=para>
The type of the
left operand of the <tt CLASS=literal>instanceof</tt>
operator must be a reference type, or a compile-time error occurs.

<P CLASS=para>
All objects inherit a method called <tt CLASS=literal>equals()</tt>
from the <tt CLASS=literal>Object</tt> class. The <tt CLASS=literal>equals()</tt>
method defined in the <tt CLASS=literal>Object</tt> class returns <tt CLASS=literal>true</tt>
if the two objects being compared are the same object. For some
classes, it is more appropriate to override the <tt CLASS=literal>equals()</tt>
method so that it compares the contents of two objects. Before
such a method can do the comparison, it should verify that the objects
are instances of the same class by using <tt CLASS=literal>instanceof</tt>.
For example, let's suppose that you are defining a class to represent
complex numbers. Since you want the <tt CLASS=literal>equals()</tt>
method to compare the contents of complex number objects, you define
an <tt CLASS=literal>equals</tt> method for the complex number class
that looks like this:

<DIV CLASS=screen>
<P>
<PRE>
boolean equals (Object o) {
   if (o instanceof complexNumber)
       return o.real == this.real 
           &amp;&amp; o.imaginary == this.imaginary;
}
</PRE>
</DIV>

<P CLASS=para>
The <tt CLASS=literal>instanceof</tt>
operator can also be used to find out if an object is an instance
of a class that implements an interface. For example:

<DIV CLASS=screen>
<P>
<PRE>
if (o instanceof Runnable)
    (new Thread((Runnable)o).start;
</PRE>
</DIV>

<P CLASS=para>
<b>References</b>
<A HREF="ch04_04.htm#JLR2-CH-4-SECT-4.5">Casts</A>;
<A HREF="ch03_02.htm#JLR2-CH-3-SECT-2.1">Class Types</A>;
<A HREF="ch03_02.htm#JLR2-CH-3-SECT-2.2">Interface Types</A>

</DIV>

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch04_07.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch04_09.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>Shift Operators</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>Equality Comparison Operators</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
